#pragma once

#include <PSDFCore/PSDFCore.h>

class Measure2D :
	public LayerPlugin,
	public EventHandler
{
	struct Position 
	{
		double lon;
		double lat;
	};

	enum MeasureStatus
	{
		MEASURE_ENABLE,	// enabled
		MEASURE_POINT,	// clicked left mouse	
		MEASURE_MOVE,	// moving the mouse
		MEASURE_END		// clicked right mouse to end current session
	};

public:
	Measure2D(void);
	~Measure2D(void);

	bool	initPlugin();

	string	getModuleName() { return "Measure2D_Lib"; }
	void	handleSystemEvent( unsigned eventType, int param1, int param2 );
	
private:
	void	onLButtonDown(short mouseScrX, short mouseScrY);
	void	onRButtonDown();
	void	onMouseMove(short mouseScrX, short mouseScrY);

	double	calcDistance(const Position& pos1, const Position& pos2);	
	void	clearScreen();

private:
	Position				_lastPosition;
	Position				_currPosition;

	float					_lastDistance;

	MeasureStatus			_currStatus;

	ref_ptr<Group>			_measureGroup; // lines and labels
	ref_ptr<Group>			_labels;
	ref_ptr<Geometry>		_lines;
	ref_ptr<Vec3Array>		_linePoints;
	ref_ptr<DrawArrays>		_lineStrip;

	static unsigned char	_mouseModeMeasure;
};

EXPORT_PLUGIN_FUNCTION
{
	return new Measure2D;
};